# encoding: utf-8
"""
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    created by lane.chang on '17/10/2024'
    comment: 服务流程
"""
from fastapi import APIRouter, Request, Body

from config import Config
from project.core.function import json_response
from project.lib.decorator import api_common, exec_duration
from project.micro_service.apollo import Apollo
from project.service.agent import AgentService

record_router = APIRouter(prefix='/c', tags=['cariabot.agent'])


@record_router.get('/agent/user/memories/init', summary='清除设备记忆')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def do_user_memories_init(request: Request, device=None):
    """
    **desc**: 清除设备记忆

    **url**: http(s)://{0}.udicaria.com.udicaria.com/c/agent/user/memories/init

    **method**: POST

    **params**:

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data:
            exec_time: 接口后端处理时长
        }
    """
    await Apollo.init_user_memories(dev_id=device.dev_id)

    return json_response()


@record_router.post('/agent/global/intent', summary='用户意图(全局)')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def get_agent_global_intent(request: Request,
                                  user_message: str = Body('', title='用户消息'),
                                  env: str = Body('normal', title='环境 normal(正常)/demo(演示)'),
                                  memories_record: bool = Body(True, description='是否添加记忆'),
                                  session=None,
                                  device=None):
    """
    **desc**: 获取全局意图

    **url**: http(s)://{0}.udicaria.com/c/agent/global/intent

    **method**: POST

        <body>:
            user_message: 用户消息
            env: normal(正常)/demo(演示)

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                intent: 意图 service(服务流程)/chat(聊天)/video_link(视频/语音通话)/report(睡眠报告)
                action: 对应的服务名称/chat(聊天)
                flow_code: 流程编号
                node_code: 节点编号
                response: 数字人话术
            }
            exec_time: 接口后端处理时长
        }
    """
    result = await AgentService.get_global_intent(session, device, user_message, env=env, memories_record=memories_record)

    return json_response(data=result)


@record_router.post('/agent/collect/advice', summary='搜集建议')
@api_common(doc_replace=(('{0}', Config.ENV_DOC_FIX),), dev_info=True)
@exec_duration(add_return=True)
async def do_agent_collect_advice(request: Request,
                                  user_message: str = Body('', title='用户消息'),
                                  env: str = Body('normal', title='环境 normal(正常)/demo(演示)'),
                                  session=None,
                                  device=None):
    """
    **desc**: 搜集建议

    **url**: http(s)://{0}.udicaria.com/c/agent/collect/advice

    **method**: POST

        <body>:
            user_message: 用户消息
            env: normal(正常)/demo(演示)

    **return**:json数据结构::

        {
            code: 0(成功)/非0(失败)
            message: 成功返回success, 否则返回error信息
            data: {
                intent: 意图 service(服务流程)/chat(聊天)/video_link(视频/语音通话)/report(睡眠报告)
                action: 对应的服务名称/chat(聊天)
                flow_code: 流程编号
                node_code: 节点编号
                response: 数字人话术
            }
            exec_time: 接口后端处理时长
        }
    """
    result = await AgentService.get_global_intent(session, device, user_message, env)

    return json_response(data=result)
